package com.polaris.live.utils

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ActivityUtils
import com.polaris.live.ui.live.activity.LiveSpectatorActivity
import com.polaris.live.ui.party.activity.PartySpectatorActivity
import com.polaris.live.ui.party.activity.game.PartyGameSpectatorActivity

/**
 * 辅助处理activity
 * @author Created by 天晴 on 2024/4/10 10:21
 * @since 1.0.0
 **/
object XActivityUtils {

    //如果是游戏房 派对房 直播间观众页 只能存在一个
    private val activityClass = setOf(
        LiveSpectatorActivity::class.java,
        PartyGameSpectatorActivity::class.java,
        PartySpectatorActivity::class.java
    )

    /**
     * 关闭这个activity上面的activity 回到当前activity
     */
    fun killTopActivityTo(activity: AppCompatActivity) {
        if (ActivityUtils.getTopActivity() !== activity) {
            val list = ActivityUtils.getActivityList()
            val index = list.indexOfFirst { ac ->
                ac === activity
            }
            if (index != -1) {
                list.subList(0, index).forEach { ac -> ac.finish() }
            }
        }
    }


    fun killOtherRoomActivity(mainClass: Class<out Activity>) {
        activityClass.forEach {
            if (mainClass != it) {
                ActivityUtils.finishActivity(it)
            }
        }
    }

}